package com.briup.srms.sms.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.srms.sms.bean.Train;
import com.briup.srms.sms.dao.TrainMapper;
import com.briup.srms.sms.dao.extend.TrainExtendMapper;
import com.briup.srms.sms.service.ITrainService;
import com.jacky.base.utils.CustomerException;
import com.jacky.base.utils.PageVM;

@Service
public class TrainServiceImpl implements ITrainService {
	@Autowired
	private TrainMapper trainMapper;
	@Autowired
	private TrainExtendMapper trainExtendMapper;
	
	@Override
	public PageVM<Train> pageQuery(int page, int pageSize, Long channelId) {
		List<Train> list = trainExtendMapper.query(page, pageSize,channelId);
		long total = trainExtendMapper.count(channelId);
		return new PageVM<>(page, pageSize, total, list);
	}

	@Override
	public void saveOrUpdate(Train train) throws CustomerException {
		if(train.getId() == null) {
			train.setStatus("进行中");
			trainMapper.insert(train);
		} else {
			trainMapper.updateByPrimaryKey(train);
		}
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		Train train = trainMapper.selectByPrimaryKey(id);
		if(train == null) {
			throw new CustomerException("要删除的数据不存在");
		}
		trainMapper.deleteByPrimaryKey(id);
	}
}
